<?php
include("../fpdf/fpdf.php");
include("../db_connect.php");

class PDF extends FPDF
{
//include 
//setting the statements in loaddata()


	//$m_logo = "";
	//$m_logo = '../../images/mlogo.png';
	
	// Page header
	function Header()
	{
		$this->Image('../images/logo.png',70,5,70,20);
		// Arial bold 15
		$this->SetFont('Arial','B',15);
		// Move to the right
		$this->Cell(1);
		//set the text color
		
		// Title cell(x,y,title,border,?,?);
		$this->Cell(210,30,'Calumpit, Bulacan',0,0,'C');
		// Line break
		$this->Ln(20);	
		
		$this->SetFont('Arial','I',12);
		$this->Cell(60,25,'Basci Auto Sales User lists',0,0,'C');
		$this->Ln(20);	
	}

	// Page footer
	function Footer()
	{
		// Position at 1.5 cm from bottom
		$this->SetY(-15);
		// Arial italic 8
		$this->SetFont('Arial','',8);
		// Page number
		$this->Cell(0,10,'Page '.$this->PageNo().' Student Information List',0,0,'C');
		
	}
	
	function LoadData(){
		include("../db_connect.php");	
		
		$fname = "";
		$lname ="";
		$sStatus = "";
		 $find[] = ',';
		$replace[] = ' ';
		if(isset($_GET['userlimit'])){
			$userlimit = mysql_real_escape_string(htmlentities(trim( $_GET['userlimit'])));
			
		}
		if(isset($_GET['fname'])){
			$fname =  mysql_real_escape_string(htmlentities(trim($_GET['fname'])));
			$fname =  str_replace($find, $replace, $fname);
		}
		if(isset($_GET['lname'])){
			$lname= mysql_real_escape_string(htmlentities(trim( $_GET['lname'])));
			$lname = str_replace($find, $replace, $lname);
		}
		if(isset($_GET['sStatus'])){
			$sStatus = mysql_real_escape_string(htmlentities(trim( $_GET['sStatus'])));
		}
		$sql = "SELECT * FROM tbl_account where type !=1 ";

		if(!empty($fname) || $fname !="" ){
		$sql .= " AND fname like '".$fname."%' ";
		}
		if(!empty($lname) || $lname !="" ){
		$sql .= " AND lname like '".$lname."%' ";
		}

		if(!empty($sStatus) || $sStatus !="" ){
			$sql .= " AND isaprove = ".$sStatus." ";
		}
		$sql .= " order by isaprove,lname DESC";
		if(!empty($userlimit) || is_numeric($userlimit)){
			
			$sql .= " limit " .$userlimit;
		}
		$result = mysql_query($sql);
		$data = array();
		
		while($row=mysql_fetch_row($result)){ 
			$data[] = $row;
		}
		return $data;
		
		
	}


	//Colored table
	// Colored table
	function FancyTable($header, $data){
		
	//Colors, line width and bold font
			//Colors, line width and bold font
			$this->SetFillColor(70,70,70);
			$this->SetTextColor(255);
			$this->SetDrawColor(128,0,0);
			$this->SetLineWidth(.3);
			$this->SetFont('Arial','B',8);
			
			//Header
			$w=array(10,32,32,30,45,45);
			$type="";
			for($i=0;$i<count($header);$i++)
			$this->Cell($w[$i],4,$header[$i],1,0,'C',true);
			$this->Ln();
			//Color and font restoration
			$this->SetFillColor(225,235,255);
			$this->SetTextColor(0);
			$this->SetFont('Arial','B',8);
			//Data
			$fill=false;
			foreach($data as $row)
			{
				$type = $this->checktype($row[10]);
				$this->Cell($w[0],5,$type,'LR',0,'L',$fill);
				$this->Cell($w[1],5,$row[3],'LR',0,'L',$fill);
				$this->Cell($w[2],5,$row[4],'LR',0,'L',$fill);
				$this->Cell($w[3],5,$row[7],'LR',0,'L',$fill);
				$this->Cell($w[4],5,$row[6],'LR',0,'L',$fill);
				$this->Cell($w[5],5,$row[8],'LR',0,'L',$fill);
				$this->Ln();
				
				$fill=!$fill;
			}
			$this->Cell(array_sum($w),0,'','T');
		$this->Ln(10);
		
	}
	function checktype($ctype) {
		if($ctype== '1'){
			$ctype = 'Active';
		}
		else if($ctype == '2'){
			$ctype = 'Pending';
		}
		else{
			$ctype = 'Block';
		}
		return $ctype;
	}

}


$pdf=new PDF();


//Column titles
$header=array('Status','First Name','Last Name','Contact','Adress','Email');
//Data loading
$data=$pdf->LoadData();

$pdf->AddPage();
$pdf->SetFont('Arial','',10);
// Move to the right
//$pdf->SetMargins(0.5, 0.5);
$pdf->Cell(1);
$pdf->SetFont('Arial','B',10);

$pdf->Ln();
$pdf->FancyTable($header,$data);
$pdf->Output();

mysql_close($con);
?>